package org.example.wikwok.service.impl;

import org.example.wikwok.common.ReturnedMessage;
import org.example.wikwok.mapper.*;
import org.example.wikwok.pojo.*;
import org.example.wikwok.service.InteractionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Notification;
import java.sql.Timestamp;

//处理视频的点赞、评论和分享逻辑。
@Service
public class InteractionServiceImpl implements InteractionService {
    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private LikeMapper likeMapper;
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private Secondary_CommentMapper secondary_commentMapper;
    @Autowired
    private OperationLogMapper operationLogMapper;

    @Override
    public void createThumbUpNotification(String userId, String commentId) {
    }

    @Override
    public boolean isThumbUp(String userId, String videoId) {
        return false;
    }

    @Override
    public boolean isFollowed(String userId, String followerId) {
        return false;
    }

    @Override
    public Notification[] queryNotificationList(String userId) {
        return new Notification[0];
    }

    @Override
    public Notification createFollowNotification(String userId, String followerId) {
        return null;
    }

    @Override
    public boolean follow(int userId, String followedId) {
        int followedid=Integer.parseInt(followedId);
        try{
            followMapper.insert(new Follow(userId, followedid));
            operationLogMapper.insert(OperationLog.build("User follow user ", userId,followedid+"_"+  ReturnedMessage.USERFOLLOWSUCCESS));
        }catch(Exception e){
            operationLogMapper.insert(OperationLog.build("User follow user ", userId,followedid+"_"+  ReturnedMessage.USERFOLLOWFAILED));
            return false;
        }
        return true;
    }

    @Override
    public boolean unfollow(int userId, String followedId) {
        int followedid=Integer.parseInt(followedId);
        try{
            followMapper.deleteByFollowerIdAndUserId(userId, followedid);
            operationLogMapper.insert(OperationLog.build("User unfollow user ", userId,followedid+"_"+  ReturnedMessage.USERUNFOLLOWSUCCESS));
        }catch(Exception e){
            operationLogMapper.insert(OperationLog.build("User unfollow user ", userId,followedid+"_"+  ReturnedMessage.USERUNFOLLOWFAILED));
            return false;
        }
        return true;
    }

    @Override
    public boolean unlike(int userId, String videoId) {
        int videoid=Integer.parseInt(videoId);
        try{
            likeMapper.deleteByVideoIdAndUserId(videoid, userId);
            operationLogMapper.insert(OperationLog.build("User unlike video ", userId,videoid+"_"+  ReturnedMessage.VIDEOUNLIKESUCCESS));
        }catch(Exception e){
            operationLogMapper.insert(OperationLog.build("User unlike video ", userId,videoid+"_"+  ReturnedMessage.VIDEOUNLIKEFAILED));
            return false;
        }
        return true;
    }
    @Override
    public boolean like(int userId, String videoId) {
        int videoid=Integer.parseInt(videoId);
        try{
            likeMapper.insert(new Like(videoid,userId,new Timestamp(System.currentTimeMillis())));
            operationLogMapper.insert(OperationLog.build("User like video ", userId,videoid+"_"+  ReturnedMessage.VIDEOLIKESUCCESS));
        }catch(Exception e){
            operationLogMapper.insert(OperationLog.build("User like video ", userId,videoid+"_"+  ReturnedMessage.VIDEOLIKEFAILED));
            return false;
        }
        return true;
    }

    @Override
    public boolean collect(int userId, String videoId) {
        int videoid=Integer.parseInt(videoId);
        try{
            operationLogMapper.insert(OperationLog.build("User collect video ", userId,videoid+"_"+  ReturnedMessage.VIDEOCOLLECTEDSUCCESS));
            collectMapper.insert(new Collect( videoid,userId,new Timestamp(System.currentTimeMillis())));
        }catch(Exception e){
            operationLogMapper.insert(OperationLog.build("User collect video ", userId,videoid+"_"+  ReturnedMessage.VIDEOCOLLECTEDFAILED));
            return false;
        }
        return true;
    }

    @Override
    public boolean uncollect(int userId, String videoId) {
        int videoid=Integer.parseInt(videoId);
        try{
            operationLogMapper.insert(OperationLog.build("User uncollect video ", userId,videoid+"_"+  ReturnedMessage.VIDEOUNCOLLECTEDSUCCESS));
            collectMapper.deleteByCollectorIdAndVideoId(userId, videoid);
        }catch(Exception e){
            operationLogMapper.insert(OperationLog.build("User collect video ", userId,videoid+"_"+  ReturnedMessage.VIDEOUNCOLLECTEDFAILED));
            return false;
        }
        return true;
    }

    @Override
    public boolean comment(int userId, String videoId, String content) {
        int videoid=Integer.parseInt(videoId);
        try{
            commentMapper.insert(new Comment(videoid,userId,new Timestamp(System.currentTimeMillis()),content));
            operationLogMapper.insert(OperationLog.build("User comment video ", userId,videoid+"_"+content));
        }catch(Exception e){
            operationLogMapper.insert(OperationLog.build("User comment video ", userId,videoid+"_"+  ReturnedMessage.COMMENTFAILED));
            return false;
        }
        return true;
    }

    @Override
    public boolean uncomment(int userId, String videoId, String commentId) {
        int commentid=Integer.parseInt(commentId);
        try{
            secondary_commentMapper.deleteByCommentId(commentid);
            commentMapper.deleteByPrimaryKey(commentid);
            operationLogMapper.insert(OperationLog.build("User uncomment video ", userId,commentid+"_"+ReturnedMessage.COMMENTDELETESUCCESS));
        }catch(Exception e){
            operationLogMapper.insert(OperationLog.build("User uncomment video ", userId,commentid+"_"+ReturnedMessage.COMMENTDELETEFAILED));
            return false;
        }
        return true;
    }

    @Override
    public boolean secendComment(int userId, String commentId, String content) {
        int commentid=Integer.parseInt(commentId);
        try{
            secondary_commentMapper.insert(new Secondary_Comment(commentid,userId,content,new Timestamp(System.currentTimeMillis())));
            operationLogMapper.insert(OperationLog.build("User comment comment ", userId,commentid+"_"+content));
        }catch(Exception e){
            operationLogMapper.insert(OperationLog.build("User comment comment ", userId,commentid+"_"+ReturnedMessage.SECENDCOMMENTFAILED));
            return false;
        }
        return true;
    }

    @Override
    public boolean unsecendComment(int userId, String commentId) {
        int commentid=Integer.parseInt(commentId);
        try{
            secondary_commentMapper.deleteByPrimaryKey(commentid);
            operationLogMapper.insert(OperationLog.build("User uncomment comment ", userId,commentid+"_"+ReturnedMessage.SECENDCOMMENTDELETESUCCESS));
        }catch(Exception e){
            operationLogMapper.insert(OperationLog.build("User uncomment comment ", userId,commentid+"_"+ReturnedMessage.SECENDCOMMENTDELETEFAILED));
            return false;
        }
        return true;
    }
}
